<?php
	$root = $_SERVER['DOCUMENT_ROOT'];
	include($root . "/util/session.php");//checks that the user is logged in
	include($root . "/util/privilege_check.php");
	checkPrivilege("student");
	$query = "SELECT * FROM results join users on users.username=results.studentUser";
	$result = mysqli_query($db,$query);
	if(mysqli_num_rows($result) < 1){
		$error = "No exams done yet.";
	}
?>

<html>
	<head>
		<title>Check results from exams</title>
		<link rel="stylesheet" type="text/css" href="studentStyle.css">
		<style>
		#results {
		  	font-family: Arial, Helvetica, sans-serif;
		 	border-collapse: collapse;
		  	width: 100%;
		  	font-size: 24px;
		}

		#results td, #results th {
  			border: 1px solid #ddd;
  			padding: 8px;
  			text-align: center;
  			background-color: white;
		}

		#results th {
			padding-top: 12px;
			padding-bottom: 12px;
			text-align: center;
			background-color: #4CAF50;
			color: white;
		}

		#results input[type=submit]{
			background: #016ABC;
			color: #fff;
			border: 1px solid #eee;
			border-radius: 20px;
			box-shadow: 5px 5px 5px #eee;
			text-shadow:none;
		}

		#results input[type=submit]:hover{
			background: #B9DFFF;
			color: #fff;
			border: 1px solid #eee;
			border-radius: 20px;
			box-shadow: 5px 5px 5px #eee;
			text-shadow:none;
		}



		</style>
	</head>
	<body>
		<?php
			include($root . "/student/header.php")
		?>
		<li><a href="/student/student.php">Back</a></li>

		<table border="1px" id="results">
			<thead>
				<tr>
					<th>Subject</th>
					<th>Chapter</th>
					<th>Score</th>
					<th>Maximum possible score</th>
					<th>Check exam</th>
				</tr>
			</thead>
			<tbody>
				<?php
					while($row=mysqli_fetch_assoc($result)){?>
					<tr>
						<?php if($row['studentUser'] == $_SESSION['login_user']){?>
							<td><?php echo $row['subjectID']; ?></td>
							<td><?php echo $row['chapterID']; ?></td>
							<td><?php echo $row['score']; ?></td>
							<td><?php echo $row['maxPoints']; ?></td>
							<?php
							$actualChapter = $row['chapterID'];
							$query2 = "SELECT * FROM tests WHERE chapterID = $actualChapter";
							$result2 = mysqli_query($db,$query2);
							$row2 = mysqli_fetch_assoc($result2);
							if($row2['revisionAllowed'] == 1){
							?>
								<td><form action="/student/reviewResults.php" method=POST>
									<input type="hidden" name = "subjectID" value="<?php echo $row['subjectID']?>">
									<input type="hidden" name = "chapterID" value=<?php echo $row['chapterID']?>>
									<input type="submit" value="Check exam"></form>
								</td>
							<?php
							}else{?>
								<td>
									<b>Revision not allowed</b>
								</td>
							<?php
							}
							?>
						<?php }?>
					</tr>
				<?php }?>
			</tbody>
		</table>

		<?php
			include($root . "/student/footer.php")
		?>
	</body>
</html>
